package hn.cch.hive.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;


@Description(
        name="custom_udf",
        value="custom_udf(): desc",
        extended = "example: usage"
)
public class CustomUDF extends UDF {
    public String evaluate(String... args) {
        if (args == null || args.length == 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String arg : args) {
            stringBuilder.append(arg);
        }
        return stringBuilder.toString();
    }

    public int evaluate(IntWritable... args) {
        if (args == null || args.length == 0) {
            return 0;
        }
        long sum = 0;
        for (IntWritable arg : args) {
            sum = Math.addExact(sum, arg.get());
        }
        return (int) sum;
    }
}
